<?php
namespace hid\base;

use hconnection\ConnectionManager;

/**
 * id存储器
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 */
abstract class IdStorage
{
    protected $storeKey = '{name}';

    /**
     * 初始化缓存
     * @var array
     */
    protected $initValueCache = [];
    
    public function __construct(array $config = [])
    {
        if (!empty($config)) {
            foreach ($config as $key => $value) {
                if (property_exists($this, $key)) {
                    $this->$key = $value;
                }
            }
        }
    }

    protected function hasInitValue(string $key):bool
    {
        return isset($this->initValueCache[$key]);
    }

    protected function addInitValueCache(string $key):void
    {
        $this->initValueCache[$key] = true;
    }

    public function getStoreKey():string
    {
        return $this->storeKey;
    }

    /**
     * 读取指定长度的id
     * @param IdBuilder $idBuilder
     * @param int $step
     * @return int|null
     */
    abstract public function incrBy(IdBuilder $idBuilder,int $step = 1):?int;

    public function getNextId(IdBuilder $idBuilder):?int
    {
        return $this->incrBy($idBuilder,$idBuilder->getStep());
    }

}
